package tech.aistar.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.ognl.IntHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.aistar.entity.Classs;
import tech.aistar.entity.Post;
import tech.aistar.entity.PostVo;
import tech.aistar.entity.UserInfo;
import tech.aistar.mapper.ClasssMapper;
import tech.aistar.mapper.PostMapper;
import tech.aistar.mapper.UserInfoMapper;
import tech.aistar.service.IPostService;

import java.util.*;

/**
 * Created by Administrator on 2019/10/8.
 */
@Service
@Transactional
public class PostServiceImpl implements IPostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private ClasssMapper classsMapper;

    @Override
    public List<PostVo> findAllPost() {
        List<Post> postList = postMapper.findAllPost();
        List<PostVo> postVoList = new ArrayList<>();
        for (Post p:postList){
            Integer userId = p.getUserId();
            UserInfo userInfo = userInfoMapper.findById(userId);

            Integer classId = p.getClassId();
            Classs c = classsMapper.findClassById(classId);

            PostVo postVo = new PostVo(p,userInfo,c);
            postVoList.add(postVo);
        }
        return postVoList;
    }

    @Override
    public List<Post> findAllPost1() {
        return postMapper.findAllPost();
    }

    @Override
    public Post findPostByPostId(Integer postId) {
        return postMapper.findPostByPostId(postId);
    }

    @Override
    public PostVo findPostByPostId1(Integer postId) {
        Post po = postMapper.findPostByPostId(postId);
        Integer userId = po.getUserId();
        UserInfo userInfo = userInfoMapper.findById(userId);
        Integer classId = po.getClassId();
        Classs c = classsMapper.findClassById(classId);
        PostVo postVo = new PostVo(po,userInfo,c);
        return postVo;
    }

    @Override
    public List<Post> findPostByThemeAndClass(Integer themeId, Integer classId) {
        return postMapper.findPostByThemeAndClass(themeId,classId);
    }



    @Override
    public Map<String,Object> findPostByTheme(Integer themeId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        //帖子的获取
        List<Post> postList2 = postMapper.findPostByTheme(themeId);

        //分页数据 - 第三方实体类存入
        PageInfo<Post> pageInfo = new PageInfo<>(postList2);
        //获取分页后的帖子集合
        List<Post> postList = pageInfo.getList();

        //存储帖子和用户信息
        List<PostVo> postVos = new ArrayList<>();
        //遍历添加用户信息
        Iterator<Post> iter = postList2.iterator();
        while (iter.hasNext()){
            Post p = iter.next();
            //获取用户的id
            Integer userId = p.getUserId();
            //获取用户的id
            UserInfo u = userInfoMapper.findById(userId);
            //获取分类名字
            Integer classId = p.getClassId();
            Classs c = classsMapper.findClassById(classId);


            //将用户信息和帖子信息放入到第三方实体类中
            PostVo postVo = new PostVo();
            postVo.setPost(p);
            postVo.setUserInfo(u);
            postVo.setClasss(c);
            //将第三方实体类放入到集合中
            postVos.add(postVo);

        }

        //使用map集合存储分页集合和第三方实体类
        Map<String,Object> map = new HashMap<>();
        map.put("pageInfo",pageInfo);
        map.put("postVos",postVos);
        System.err.println(postVos);
        return map;


        //获取帖子集合
//        List<PostVo> postVoList2 = new ArrayList<>();
//        for (Post p2:postList2){
//            Integer userId = p2.getUserId();
//            UserInfo userInfo = userInfoMapper.findById(userId);
//            PostVo postVo = new PostVo(p2,userInfo);
//            postVoList2.add(postVo);
//        }
//        return postVoList2;
    }


    @Override
    public Long findPostNumByTheme(Integer themeId) {
        return postMapper.findPostNumByTheme(themeId);
    }

    @Override
    public List<Post> findAllPostByUserId(Integer userId) {
        return postMapper.findAllPostByUserId(userId);
    }

    @Override
    public void savePost(Post post) {
        postMapper.savePost(post);
    }

    @Override
    public void delPost(Integer postId) {
        postMapper.delPost(postId);
    }

    @Override
    public UserInfo findNameImg(Integer postId) {
        return postMapper.findNameImg(postId);
    }
}
